from django.db.models import Q
from django.shortcuts import render
from django.views.generic import View
from pure_pagination import Paginator, EmptyPage, PageNotAnInteger

from apps.organizations.models import Org,City,Teacher
from apps.courses.models import Course
from apps.operations.models import UserFavorite
from apps.users.utils.mixin import LoginRequiredMiXin


class OrgPageView(View):
    """
    授课机构-分页
    """
    def get(self,request):
        # 机构所属城市
        citys = City.objects.all()
        # 机构
        orgs = Org.objects.all()
        # 热门机构
        orgs_hot = orgs.order_by("-clickNum")[:3]
        # 全局搜索关键词
        keywords = request.GET.get("keywords","")
        if keywords:
            orgs = orgs.filter(Q(name__icontains=keywords)|Q(desc__icontains=keywords))
        # 分页参数(城市\机构类型\按照学习人数或者课程数进行排序)
        cityId = request.GET.get("cityId","")
        if cityId:
            orgs = orgs.filter(city_id=int(cityId))
        category = request.GET.get("category","")
        if category:
            orgs = orgs.filter(category=category)
        sort = request.GET.get("sort","")
        if sort:
            orgs = orgs.order_by("-" + sort)
        # 对机构进行分页
        try:
            page = request.GET.get("page",1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(orgs,2,request=request)
        orgPage = p.page(page)
        return render(request, "indexFororgPage.html", {"pageObj": orgPage, "citys": citys, "orgs_hot": orgs_hot,
                "cityId": cityId, "category": category, "sort": sort, "active": "org"})


class orgHomeView(LoginRequiredMiXin,View):
    """
    授课机构-首页
    """
    def get(self,request,orgId):
        # 机构
        org = Org.objects.get(id=int(orgId))
        org.clickNum += 1
        org.save()
        # 课程
        courses = Course.objects.filter(org=org)[:3]
        # 授课讲师
        teachers = Teacher.objects.filter(org=org)[:3]
        # 是否收藏该机构
        hasFav = False
        userFavorite = UserFavorite.objects.filter(user=request.user,favId=int(orgId),favType=1)
        if userFavorite:
            hasFav = True
        return render(request, "orgHomePage.html", {"org":org, "courses":courses, "teachers":teachers,
              "active":"orgHome","hasFav":hasFav})


class orgCourceView(LoginRequiredMiXin,View):
    """
    机构课程
    """
    def get(self,request,orgId):
        # 机构
        org = Org.objects.get(id=int(orgId))
        # 课程
        courses = Course.objects.filter(org=org)[:3]
        # 是否收藏该机构
        hasFav = False
        userFavorite = UserFavorite.objects.filter(user=request.user,favId=int(orgId),favType=1)
        if userFavorite:
            hasFav = True
        return render(request, "orgCourse.html", {"org":org,"courses":courses,
              "active":"orgCource","hasFav":hasFav})


class orgTeacherView(View):
    """
    机构讲师
    """
    def get(self,request,orgId):
        # 机构
        org = Org.objects.get(id=int(orgId))
        # 授课讲师
        teachers = Teacher.objects.filter(org=org)[:3]
        # 是否收藏该机构
        hasFav = False
        userFavorite = UserFavorite.objects.filter(user=request.user,favId=int(orgId),favType=1)
        if userFavorite:
            hasFav = True
        return render(request, "orgTeacher.html", {"org":org,"teachers": teachers,"active":"orgTeacher",
                      "hasFav":hasFav})


class orgDescView(View):
    """
    机构描述
    """
    def get(self,request,orgId):
        # 机构
        org = Org.objects.get(id=int(orgId))
        # 是否收藏该机构
        hasFav = False
        userFavorite = UserFavorite.objects.filter(user=request.user,favId=int(orgId),favType=1)
        if userFavorite:
            hasFav = True
        return render(request, "orgDesc.html",{"org":org,"active":"orgDesc","hasFav":hasFav})


class TeacherPageView(View):
    """
    授课教师-分页
    """
    def get(self,request):
        # 教师
        teachers = Teacher.objects.all()
        # 热门教师
        teachersHot = teachers.order_by("-clickNum")[:3]
        # 全局搜索关键词
        keywords = request.GET.get("keywords", "")
        if keywords:
            teachers = teachers.filter(Q(name__icontains=keywords)|Q(workYear__icontains=keywords)|Q(points__icontains=keywords))
        # 分页参数（点击量）
        sort = request.GET.get("sort","")
        if sort:
            teachers.order_by("-"+sort)
        # 教师分页
        try:
            page = request.GET.get("page",1)
        except PageNotAnInteger:
            page = 1
        p = Paginator(teachers,3,request=request)
        teacherPage = p.page(page)
        return render(request,"indexForTeacherPage.html",{"sort":sort,
              "pageObj":teacherPage,"teachersHot":teachersHot})



class TeacherDetailView(LoginRequiredMiXin,View):
    """
    授课讲师-详情
    """
    def get(self,request,teacherId):
        # 教师
        teacher = Teacher.objects.get(id=teacherId)
        teacher.clickNum += 1
        teacher.save()
        # 热门教师
        teachersHot = Teacher.objects.all().order_by("-clickNum")[:3]
        # 用户是否收藏机构、讲师
        hasFavTeacher = False
        hasFavorg = False
        if UserFavorite.objects.filter(user=request.user,favType=1,favId=teacher.org.id):
            hasTeacher = True
        if UserFavorite.objects.filter(user=request.user,favType=2,favId=teacher.id):
            hasFavorg = True
        return render(request, "orgTeacherDetail.html", {"hasFavTeacher":hasFavTeacher, "hasFavorg":hasFavorg,
                "teacher":teacher,"teachersHot":teachersHot})

